// 导入表单验证插件
import wxValidate from 'wechat-validate'

Page({
  behaviors: [wxValidate],
  data: {
    currentDate: new Date().getTime(),
    houseLayerVisible: false,
    repairLayerVisible: false,
    dateLayerVisible: false,
    houseList: [],
    houseInfo: '',
    houseId: '',
    repairItem: [],
    repairItemId: '',
    repairItemName: '',
    description: '123',
    mobile: '17613273956',
    appointment: '',
    attachment: []
  },
  rules: {
    houseId: [{ required: true, message: '请选择报修房屋!' }],
    repairItemId: [{ required: true, message: '请选择维修的项目!' }],
    mobile: [
      { required: true, message: '请填写手机号码!' },
      { pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号码!' }
    ],
    appointment: [{ required: true, message: '请选择预约日期!' }],
    description: [{ required: true, message: '请填写问题描述!' }]
  },
  onLoad ({ id }) {
    console.log(id)
    this.getHouseList()
    this.getRepairItem()
    // 如果有id表明是修改操作
    if (id) this.getRepairDetail(id)
  },

  // 获取房屋列表（必须是通过审核的房屋）
  async getHouseList () {
    // 调用接口
    const { data } = await wx.http.get('/house')
    // console.log(data)
    this.setData({ houseList: data })
  },
  selectHouseInfo (ev) {
    // console.log(ev)
    const { detail } = ev
    // console.log(detail)
    this.setData({ houseInfo: detail.name, houseId: detail.id })
  },

  // 获取维修项目
  async getRepairItem () {
    // 调用接口
    const { data } = await wx.http.get('/repairItem')
    // console.log(data)
    this.setData({ repairItem: data })
  },
  selectRepairItemInfo (ev) {
    const { detail } = ev
    this.setData({ repairItemName: detail.name, repairItemId: detail.id })
  },

  //日期选择
  chooseDate (ev) {
    // console.log(ev.detail)
    const time = wx.utils.dataFormat(ev.detail)
    // console.log(time)
    this.setData({ appointment: time, dateLayerVisible: false })
  },

  //图片
  uploadPicture (ev) {
    // console.log(ev)
    // 上传文件的信息
    const { file } = ev.detail
    // 调用 API 实现文件上传
    wx.uploadFile({
      filePath: file.url,
      name: 'file',
      url: wx.http.baseURL + '/upload',
      header: {
        Authorization: 'Bearer ' + getApp().token
      },
      success: res => {
        const data = JSON.parse(res.data)
        // console.log(data)
        // 先获取原来已经上传的图片
        const { attachment } = this.data
        // 追加新的上传的图片
        attachment.push(data.data)
        this.setData({ attachment })
      }
    })
  },
  openHouseLayer () {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer () {
    this.setData({ houseLayerVisible: false })
  },
  openRepairLayer () {
    this.setData({ repairLayerVisible: true })
  },
  closeRepairLayer () {
    this.setData({
      repairLayerVisible: false
    })
  },

  openDateLayer () {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer () {
    this.setData({ dateLayerVisible: false })
  },
  async submit () {
    const flag = this.validate()
    // console.log(flag)
    if (!flag) return
    // 提取接口需要的数据
    const { houseId, repairItemId, mobile, appointment, description, attachment } = this.data
    const { code } = await wx.http.post('/repair', {
      houseId,
      repairItemId,
      mobile,
      appointment,
      description,
      attachment
    })
    if (code !== 10000) return wx.utils.toast()
    // 跳转到报修列表页面
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index'
    })
  },
  //修改操作
  async getRepairDetail (id) {
    const { data: repairDetail } = await wx.http.get('/repair/' + id)
    console.log(repairDetail)
    this.setData({ ...repairDetail })
  }
})
